#!/usr/bin/env python
# -*- coding: utf-8 -*-

import cx_Oracle as cx_Oracle

pool = cx_Oracle.SessionPool(user="scott", password="tiger", dsn="localhost:1521/ADAM", min=1,
                                 max=1000, increment=1)


class OracleManager:
    """
    oracle管理类
    """

    def __init__(self):
        self.conn = None
        self.cursor = None

    def connect(self):
        self.conn = pool.acquire()
        self.cursor = self.conn.cursor()

    def execute_query(self, sql):
        self.cursor.execute(sql)
        return self.cursor.fetchall()

    def execute_query_param(self, sql, param):
        self.cursor.execute(sql, param)
        return self.cursor.fetchall()

    def execute(self, sql):
        self.cursor.execute(sql)
        return self.cursor.fetchall()

    def execute_update(self, sql):
        self.cursor.execute(sql)
        self.conn.commit()

    def batch_insert(self, sql, param_list):
        """
        批量插入数据
        :param sql:
        :param param_list:
        :return:
        """

        self.cursor.executemany(sql, param_list)
        self.conn.commit()

    def close(self):
        if self.cursor:
            self.cursor.close()
        if self.conn:
            pool.release(self.conn)

    def cursor_close(self):
        """
        关闭游标
        """

        if self.cursor:
            self.cursor.close()

    def connect_close(self):
        """
        关闭连接
        :return:
        """

        if self.conn:
            pool.release(self.conn)

    def string_parameter(self, parameter):
        """
        定义string类型的out变量
        """

        return self.cursor.var(cx_Oracle.STRING, parameter)

    def cursor_parameter(self):
        """
        定义cursor类型的out变量
        """

        return self.cursor.var(cx_Oracle.CURSOR)

    def array_parameter(self, array_parameter):
        """
        定义array类型的out变量
        """

        return self.cursor.arrayvar(cx_Oracle.DB_TYPE_OBJECT, array_parameter)
